package com.wbl.train.business.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wbl.train.common.business.dto.TrainStationDto;
import com.wbl.train.common.business.entity.TrainStationEntity;
import com.wbl.train.business.mapper.TrainStationMapper;
import com.wbl.train.business.service.TrainStationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wbl.train.common.exception.ApplicationException;
import com.wbl.train.common.page.PageVo;
import com.wbl.train.common.result.ResultCode;
import com.wbl.train.common.utils.SnowUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 火车车站 服务实现类
 * @author 王博纶
 * @since 2024-02-02
 */
@Service
public class TrainStationServiceImpl extends ServiceImpl<TrainStationMapper, TrainStationEntity> implements TrainStationService {

    @Override
    public PageVo<TrainStationEntity> queryTrainStation(TrainStationDto trainStationDto) {
        // 获取分页所需要的参数
        int page = trainStationDto.getPage();
        int limit = trainStationDto.getLimit();

        LambdaQueryWrapper<TrainStationEntity> trainStationWrapper = new LambdaQueryWrapper<TrainStationEntity>()
                        .orderByAsc(TrainStationEntity::getTrainCode)
                        .orderByAsc(TrainStationEntity::getStationIndex)
                        .eq(ObjectUtil.isNotEmpty(trainStationDto.getTrainCode()),TrainStationEntity::getTrainCode,trainStationDto.getTrainCode());

        //创建分页对象
        IPage<TrainStationEntity> trainStationPage = new Page<>(page,limit);
        //调用分页方法
        IPage<TrainStationEntity> trainStationPageData = page(trainStationPage,trainStationWrapper);
        // 获取查询出来的数据集合
        List<TrainStationEntity> records = trainStationPageData.getRecords();
        // 将数据塞到PageVo类中并返回出去
        return new PageVo<TrainStationEntity>()
                .setDataList(records)
                .setPageSize(limit).setCurrentPage(page)
                .setTotal((int) trainStationPageData.getTotal())
                .setPages((int) trainStationPageData.getPages());
    }

    @Override
    public boolean insertAndUpdateTrain(TrainStationEntity trainStation) {
        // 获取当前的时间 --- 用于创建和修改的时候的时间填充
        DateTime now = DateTime.now();
        // 设置火车车站的修改时间
        trainStation.setUpdateTime(now);
        // 判断Id是否为空
        if(ObjectUtil.isNotNull(trainStation.getId())){
            // 根据id查询火车车站信息
            TrainStationEntity trainStationEntity = this.getById(trainStation.getId());
            // 当前id对应的无火车车站信息
            if(ObjectUtil.isNull(trainStationEntity)){
                throw new ApplicationException(ResultCode.DATA_NOT_EXIST,"当前火车车站不存在");
            }
            // 更新火车车次信息
            return this.updateById(trainStation);
        }
        // 保存之前的唯一键校验
        TrainStationEntity trainStationEntity = getTrainStationByUniqueKey(trainStation.getTrainCode(),trainStation.getStationIndex());
        // 查询出的trainEntity不为空，不允许新增
        if(ObjectUtil.isNotEmpty(trainStationEntity)){
            throw new ApplicationException(ResultCode.DATA_EXIST,"当前火车车次已存在");
        }
        // 给新增的火车车站设置Id，创建时间和修改时间
        trainStation.setId(SnowUtil.getSnowflakeNextId())
                .setCreateTime(now);
        // 新增火车车站
        return this.save(trainStation);
    }

    /**
     * 根据唯一键查询 trainStation 火车车站信息
     * @param trainCode 火车车次编号
     * @param stationIndex 车站序号
     * @return trainStation 火车车站信息
     */
    private TrainStationEntity getTrainStationByUniqueKey(String trainCode,Integer stationIndex) {
        // 通过trainCode和stationIndex进行火车车站信息联合主键，进行去重查询
        LambdaQueryWrapper<TrainStationEntity> wrapper = new LambdaQueryWrapper<TrainStationEntity>()
                .eq(TrainStationEntity::getTrainCode, trainCode)
                .eq(TrainStationEntity::getStationIndex, stationIndex);
        return this.getOne(wrapper);
    }

    /**
     * 根据trainCode查询 火车车站信息
     * @param trainCode 火车车次编号
     * @return trainStation 火车车站信息
     */
    public List<TrainStationEntity> selectByTrainCode(String trainCode) {
        // 通过trainCode和stationIndex进行火车车站信息联合主键，进行去重查询
        LambdaQueryWrapper<TrainStationEntity> wrapper = new LambdaQueryWrapper<TrainStationEntity>()
                .eq(TrainStationEntity::getTrainCode, trainCode)
                .orderByAsc(TrainStationEntity::getStationIndex);
        return this.list(wrapper);
    }
}
